package cn.ffcs.unm.staff.component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.Date;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.ListModelList;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.raptornuke.portal.PortalException;
import cn.ffcs.raptornuke.portal.SystemException;
import cn.ffcs.uec.common.util.EnvPath;
import cn.ffcs.uec.corp.manager.UecPsnManager;
import cn.ffcs.unm.staff.component.bean.StaffBaseInfoListboxBean;
import cn.ffcs.unm.staff.manager.StaffNeimBaseInfoManager;
import cn.ffcs.unm.staff.model.StaffBaseInfo;
import cn.ffcs.uom.common.key.ActionKeys;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * 人员基本信息
 * 
 * @Title:
 * @Description:
 * @author Wang Yiren
 * @版权 FFCS（C） 2017
 * @date 2018年3月12日
 */
@Controller
@Scope("prototype")
public class StaffBaseInfoListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = 2905672842249269792L;

	private StaffBaseInfoListboxBean bean = new StaffBaseInfoListboxBean();

	@SuppressWarnings("unused")
	private StaffBaseInfo staffBaseInfo;

	private StaffBaseInfo qryStaffBaseInfo;

	private StaffNeimBaseInfoManager staffNeimBaseInfoManager = (StaffNeimBaseInfoManager) ApplicationContextUtil
			.getBean("staffNeimBaseInfoManager");

	private UecPsnManager uecPsnManager = (UecPsnManager) ApplicationContextUtil.getBean("uecPsnManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	private static final String zul = "/pages_nmg/views/staff_base_information/comp/staffBaseInfo_listbox_ext.zul";

	public StaffBaseInfoListboxExt() {
		// 1.绑定页面
		Executions.createComponents(zul, this, null);
		// 2.页面控件与java属性绑定
		Components.wireVariables(this, bean);
		// 3.绑定监听事件
		Components.addForwards(this, this, '$');

	}

	/**
	 * 查询
	 * 
	 * @throws ParseException
	 */
	public void onQuery() throws ParseException {
		clearVariable();
		StaffBaseInfo objStaffBaseInfo = StaffBaseInfo.newInstance();
		PubUtil.fillPoFromBean(bean, objStaffBaseInfo);
		Date monthIdDate = bean.getMonthId().getValue();
		if (monthIdDate == null) {
			ZkUtil.showError("请输入账期", "温馨提示");
			return;
		}
		int index = bean.getRadiogroup().getSelectedIndex();
		String monthId = DateUtil.getYYYYMMDD(monthIdDate);
		if (index == 0) {
			String monthIdMonth = monthId.substring(0, 6);
			objStaffBaseInfo.setMonthId(monthIdMonth);
		} else {
			objStaffBaseInfo.setMonthId(monthId);
		}
		qryStaffBaseInfo = objStaffBaseInfo;
		this.bean.getStaffListboxPaging().setActivePage(0);

		queryStaffBaseInfo();
		staffBaseInfo = null;
	}

	/**
	 * 重置
	 */
	public void onReset() {
		bean.getName().setValue(null);
		bean.getCode().setValue(null);
		bean.getMonthId().setValue(null);
		clearVariable();
		ListboxUtils.clearListbox(this.bean.getStaffListbox());
		this.bean.getStaffListboxPaging().setTotalSize(1);
	}

	/**
	 * 查询
	 */
	public void queryStaffBaseInfo() {
		if (this.qryStaffBaseInfo != null) {
			int activePage = bean.getStaffListboxPaging().getActivePage();
			int pageSize = bean.getStaffListboxPaging().getPageSize();
			PageInfo pageInfo = staffNeimBaseInfoManager.queryPageInfoByStaffBaseInfo(qryStaffBaseInfo, activePage + 1,
					pageSize);
			ListModelList dataList = new BindingListModelList(pageInfo.getDataList(), true);
			bean.getStaffListbox().setModel(dataList);
			bean.getStaffListboxPaging().setTotalSize(NumericUtil.nullToZero(pageInfo.getTotalCount()));
		}
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		qryStaffBaseInfo = null;
		staffBaseInfo = null;
	}

	/**
	 * 翻页
	 */
	public void onListboxPaging() {
		this.queryStaffBaseInfo();
	}

	/**
	 * 人员基本信息--清单下载
	 * 
	 * @Description:
	 * @author Wang Yiren
	 * @date 2018年3月12日
	 * @throws Exception
	 * @throws IOException
	 */
	public void onDownload() throws Exception, IOException {

		Date monthIdDate = bean.getMonthId().getValue();
		if (monthIdDate == null) {
			ZkUtil.showError("请选择：月表账期", "温馨提示");
			return;
		}
		// 日/月表按钮
		int index = bean.getRadiogroup().getSelectedIndex();
		// 格式化日期
		String monthId = DateUtil.getYYYYMMDD(monthIdDate);
		// index == 0 表示月
		if (index == 0) {
			// 获取年月--201801
			String monthIdMonth = monthId.substring(0, 6);
			// 获取服务器文件
			String filePath = EnvPath.proPath + monthIdMonth + "_" + "VIEW_DATALIST_BD_PSNDOC.csv";
			File file = new File(filePath);
			if (file.exists()) {
				try {
					// 设置字符集
					String charset = "UTF-8";
					// 编码后文件名
					String encodedName = null;
					encodedName = URLEncoder.encode("01人员基本信息翻译清单.csv", charset);
					// 将空格替换为+号
					encodedName = encodedName.replace("%20", "+");
					// 将文件转化为文件输入流
					FileInputStream in = new FileInputStream(file);
					// 掉zk的浏览器下载方法，进行下载
					Filedownload.save(in, "application/octet-stream", encodedName);
				} catch (FileNotFoundException e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				} catch (IOException e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				} catch (Exception e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				}
			} else {
				ZkUtil.showError("下载清单失败,请联系管理员", "温馨提示");
			}
		} else {
			ZkUtil.showError("暂不支持日表清单下载", "温馨提示");
		}
	}

	public void onDownloadNative() throws Exception, IOException {

		Date monthIdDate = bean.getMonthId().getValue();
		if (monthIdDate == null) {
			ZkUtil.showError("请选择：月表账期", "温馨提示");
			return;
		}
		// 日/月表按钮
		int index = bean.getRadiogroup().getSelectedIndex();
		// 格式化日期
		String monthId = DateUtil.getYYYYMMDD(monthIdDate);
		// index == 0 表示月
		if (index == 0) {
			// 获取年月--201801
			String monthIdMonth = monthId.substring(0, 6);
			// 获取服务器文件
			String filePath = EnvPath.proPath + monthIdMonth + "_" + "V_NATIVE_BD_PSNDOC.csv";
			File file = new File(filePath);
			if (file.exists()) {
				try {
					// 设置字符集
					String charset = "UTF-8";
					// 编码后文件名
					String encodedName = null;
					encodedName = URLEncoder.encode("01人员基本信息原表清单.csv", charset);
					// 将空格替换为+号
					encodedName = encodedName.replace("%20", "+");
					// 将文件转化为文件输入流
					FileInputStream in = new FileInputStream(file);
					// 掉zk的浏览器下载方法，进行下载
					Filedownload.save(in, "application/octet-stream", encodedName);
				} catch (FileNotFoundException e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				} catch (IOException e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				} catch (Exception e) {
					ZkUtil.showError("下载清单失败", "系统提示");
					e.getMessage();
				}
			} else {
				ZkUtil.showError("下载清单失败,请联系管理员", "温馨提示");
			}
		} else {
			ZkUtil.showError("暂不支持日表清单下载", "温馨提示");
		}
	}

	/**
	 * Set Button Permission
	 * 
	 * @param string
	 * @throws SystemException
	 * @throws PortalException
	 */
	public void setPagePosition(String page) throws PortalException, SystemException {
		boolean canView = false;
		boolean canAdd = false;
		boolean canEdit = false;
		boolean canDel = false;

		if (PlatformUtil.isAdmin()) {
			canView = true;
			canAdd = true;
			canDel = true;
			canEdit = true;
		} else if ("staffBaseInfoPage".equals(page)) {
			if (PlatformUtil.checkHasPermission(getPortletInfoProvider(), ActionKeys.STAFF_BASE_INFO_ADD)) {
				canAdd = true;
			}
			if (PlatformUtil.checkHasPermission(getPortletInfoProvider(), ActionKeys.STAFF_BASE_INFO_DEL)) {
				canDel = true;
			}
			if (PlatformUtil.checkHasPermission(getPortletInfoProvider(), ActionKeys.STAFF_BASE_INFO_EDIT)) {
				canEdit = true;
			}
			if (PlatformUtil.checkHasPermission(getPortletInfoProvider(), ActionKeys.STAFF_BASE_INFO_VIEW)) {
				canView = true;
			}
		}
		bean.getAddStaffButton().setVisible(canAdd);
		bean.getViewStaffButton().setVisible(canView);
		bean.getEditStaffButton().setVisible(canEdit);
		bean.getDelStaffButton().setVisible(canDel);
	}

}
